package Hot100;

public class searchMatrix {
    public static void main(String[] args) {
        int matrix[][] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j] + "   ");
            }
            System.out.println();
        }
        System.out.println(searchMatrix(matrix,15));
    }
    public static boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length ,n = matrix[0].length;
        int x = 0,y = n - 1;
        if(target > matrix[m-1][n-1] )
            return false;
        while (x < m && y >= 0){
            if(matrix[x][y] == target)
                return true;
            else if (matrix[x][y] > target) {
                y--;
            } else if (matrix[x][y] < target) {
                x++;
            }
        }
        return false;



    }

}
